/**
 * 部署 pay-admin
 */

const shell = require('shelljs');// 引入shell.js等依赖
const func = require('./../util/func.js');

/**生成管理后台jar包到部署目录
 * 
 * @param {*} src_path 下载源代码的根目录
 * @param {*} git_address 源代码地址
 * @param webapp_dir jar的目标目录
 * @returns project_dir  项目源代码的目录
 */
async function pay_admin(src_path, git_address, webapp_dir) {
    const project_name = func.gitProjectName(git_address);

    const project_dir = src_path + "/" + project_name;//项目路径

    func.deleteDir(project_dir);//如果旧代码存在，则删除
    //shell.mkdir('-p', project_dir);

    //第一步，摘取代码
    const clone_code = await func.git_clone(src_path, git_address);
    if (clone_code != 0) {
        console.log('拉取代码失败 ');
        shell.echo('克隆失败，请检查克隆地址是否存在');
        shell.exit(1);
    }

    //第二步，生成jar包
    const build_code = await func.bulid_jar(project_dir);
    if (build_code != 0) {
        console.log('构建jar包失败 ');
        shell.echo('构建jar包失败');
        shell.exit(1);
    }

    //第三步，复制到部署目标
    const jar = project_dir + '/target/' + func.pom_jar_name(project_dir);
    console.info(jar);
    shell.rm('-rf', webapp_dir + "/*")
    const cpResult = shell.cp('-rf', jar, webapp_dir);
    console.info(new Date() + " cp code=" + cpResult.code + (cpResult.code == 0 ? "成功" : "失败") + "复制jar包");

    return project_dir;
}

/**
 * 复制spring的yml配置文件到部署目录
 * @param {*} project_dir 源代码目录
 * @param {*} yml 配置文件名称
 * @param {*} target_dir 部署的目标目录
 */
function copy_config(project_dir, yml, target_dir) {
    const config = project_dir + "/src/main/resources/" + yml;
    const target = target_dir + "/" + yml;
    if(shell.test('-f',target)){
        shell.rm('-r', target);
    }
    const cpResult = shell.cp('-rf', config, target);
    console.info((cpResult.code == 0 ? "成功" : "失败") + "\n复制" + config + "\n到" + target);
}

/**
 * pay-admin-web 管理后台前端页面
 * 
 * @param {*} src_path 下载源代码之后存放源代码的目录
 * @param {*} project_name  项目名
 * @param {*} git_address 源代码地址
 * @param {*} static_dir 页面build之后，静态页面部署的目标
 */
async function pay_admin_web(src_path, project_name, git_address, static_dir, buildEnv) {
    const project_dir = src_path + "/" + project_name;//项目路径

    const node_module = project_dir + "/node_modules";
    const temp_dir = src_path + "/admin_node_modules";
    // node_modules 单独复制出来，避免重复下载node_module
    if (shell.test('-e', project_dir)) {//是否存在旧代码目录
        if (shell.test('-e', node_module)) {
            shell.cp(node_module, temp_dir);//把node_modules目录，移到暂存目录
        }
    }
    func.deleteDir(project_dir);//如果旧代码存在，则删除

    //第一步，摘取代码
    const clone_code = await func.git_clone(src_path, git_address);
    if (clone_code != 0) {
        console.log('拉取代码失败 ');
        shell.echo('克隆失败，请检查克隆地址是否存在');
        shell.exit(1);
    }
    if (shell.test("-e", temp_dir)) {
        shell.cp(temp_dir, node_module);//再把node_modules从暂存目录，移到源代码目录
        shell.rm("-rf", temp_dir);
    }

    //第二步，nodejs build   
    const code_i = func.bulid_npm(project_dir, "npm install");//运行 npm install
    const message_i = "install pay-admin-web" + (code_i == 0 ? "成功" : "失败");
    // code_i == 0 ? console.log(message_i) : console.error(message_i);
    shell.echo(message_i);

    const code_b = func.bulid_npm(project_dir, "npm run build:" + buildEnv);//生成目标页面
    const message_b = "build pay-admin-web" + (code_b == 0 ? "成功" : "失败");
    // code_b == 0 ? console.log(message_b) : console.error(message_b);
    shell.echo(message_b);

    //第三步，复制到目标
    shell.rm('-rf', static_dir + "/*")
    const cpResult = shell.cp('-rf', project_dir + '/dist/*', static_dir);
    console.info(" 复制pay-admin-web 到" + static_dir + (cpResult.code == 0 ? "成功" : "失败"));

}

module.exports = {
    pay_admin,
    copy_config,
    pay_admin_web
}


//测试 pay_admin_web
function test() {
    // sudo rm -rf /deploy/src/pay-admin-web
    const src_path = '/deploy/src', project_name = 'pay-web';
    const git_address = 'https://fanruifeng:taijun432@git.visioners.cn/charge/pay-web.git';
    const static_dir = '/deploy/pay_admin/static', buildEnv = 'prod';
    pay_admin_web(src_path, project_name, git_address, static_dir, buildEnv);
}

// test();